//
//  AiBaseNavigationController.m
//  fanligou
//
//  Created by allen on 2017/10/14.
//  Copyright © 2017年 allen. All rights reserved.
//

#import "AiBaseNavigationController.h"

@interface AiBaseNavigationController ()<UINavigationBarDelegate>

@end

@implementation AiBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.interactivePopGestureRecognizer.delegate = nil;
    UINavigationBar *bar = [UINavigationBar appearance];

    [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"tabbar_color"]
                             forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setTranslucent:NO];///view布局偏移，还有导航栏透明
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
    
    
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance * appearance = [[UINavigationBarAppearance alloc] init];
        // 背景色
        appearance.backgroundColor = [UIColor redColor];
        // 去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
        appearance.shadowColor = [UIColor clearColor];
        [appearance setBackgroundImage:[UIImage imageNamed:@"tabbar_color"]];
        // 字体颜色、尺寸等
        appearance.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:18]};
        // 带scroll滑动的页面
        bar.scrollEdgeAppearance = appearance;
        // 常规页面
        bar.standardAppearance = appearance;
    }
}


- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated {
    if (viewControllers.count  > 2) {
        UIViewController * viewController = viewControllers[viewControllers.count - 1];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        AppDelegate *delegate = (id)[UIApplication sharedApplication].delegate;
        
        UIImage * img = nil;
//        if ([delegate.backTpye isEqualToString:@"0"]) {
//            img = [UIImage imageNamed:@"navigationButtonReturn"];
//        }else{
//            img = [UIImage imageNamed:@"back_gray"];
//
//        }
        img = [UIImage imageNamed:@"back_white"];

        
        [button setImage:img forState:UIControlStateNormal];
        [button setImage:img forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 0, 40, 30);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        
        // 修改导航栏左边item
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super setViewControllers:viewControllers animated:animated];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (self.childViewControllers.count > 0) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        AppDelegate *delegate = (id)[UIApplication sharedApplication].delegate;
        
        UIImage * img = nil;
//        if ([delegate.backTpye isEqualToString:@"0"]) {
//            img = [UIImage imageNamed:@"navigationButtonReturn"];
//        }else{
//            img = [UIImage imageNamed:@"navigationButtonReturnClick"];
//
//        }
        img = [UIImage imageNamed:@"back_white"];
        [button setImage:img forState:UIControlStateNormal];
        [button setImage:img forState:UIControlStateHighlighted];
        button.frame = CGRectMake(0, 0, 40, 30);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        [button addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        // 修改导航栏左边item
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        viewController.hidesBottomBarWhenPushed = YES;
    }

    [super pushViewController:viewController animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)backAction {
    [self popViewControllerAnimated:YES];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
